package com.zhangjianbing.hystrix.config;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangjianbing
 * @since 2021/2/20
 */
@Component
public class CustomThreadPool {

    private final int cpu = Runtime.getRuntime().availableProcessors();
    private final int corePoolSize = cpu + 1;
    private final int maximumPoolSize = cpu * 2 + 1;

    @Bean
    public ExecutorService executorService() {
        long keepAliveTime = 1L;
        int maxQueueNum = 50000;
        return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, new ArrayBlockingQueue<>(maxQueueNum));
    }

}
